package uni.UNIBEE7B8D.Presenter;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;

import org.json.JSONException;

import java.text.ParseException;

import uni.UNIBEE7B8D.CommonTool.HttpUrl;
import uni.UNIBEE7B8D.CommonTool.OkHttpUtil;
import uni.UNIBEE7B8D.Enty.Result;
import uni.UNIBEE7B8D.view.User.LoginUserPwd.LoginUserPwdContract;

public class LoginUserPersenter implements LoginUserPwdContract.LoginUserPersenter {
    LoginUserPwdContract.LoginUserPwdView loginView;

    public LoginUserPersenter(LoginUserPwdContract.LoginUserPwdView loginView) {
        this.loginView = loginView;
    }

    @Override
    public void login(String user, String pwd) {
        if(TextUtils.isEmpty(user)){
            loginView.Toast("请输入用户名");
        }else if(TextUtils.isEmpty(pwd)){
            loginView.Toast("请输入密码");
        }else{
            new OkHttpUtil().getJson(HttpUrl.loginByAccount + "?account=" + user + "&pwd=" + HttpUrl.md5(pwd+"JSKF1234")+"&v_num=1.0.0", new OkHttpUtil.HttpCallBack() {
                @Override
                public void onSusscess(String data) throws JSONException, ParseException {
                    Log.d("==",data);
                    Result result = new Gson().fromJson(data,Result.class);
                    if (result.isSuccess()){
                        loginView.login(result.getResult());
                    }else{
                        loginView.Toast(result.getMessage());
                    }
                }
                @Override
                public void onError(String meg) {
                    super.onError(meg);
                    loginView.Toast(meg);
                }
            });
        }
    }

}
